package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.entity.Carousel;
import com.hotel.mapper.CarouselMapper;
import com.hotel.service.AliOssService;
import com.hotel.service.CarouselService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.vo.CarouselVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 广告信息 服务实现类
 * </p>
 *
 * @author Lin
 * @since 2022-05-23
 */
@Service
public class CarouselServiceImp extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Override
    public IPage<Carousel> getCarouselList(String carouselTitle,Integer status,Integer current,Integer size) {
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        Page<Carousel> carouselPage = new Page<>(current, size);
        IPage<Carousel> carouselIPage = null;
        if (StringUtils.isNotEmpty(carouselTitle)){
            wrapper.like("carousel_title",carouselTitle);
        }
        if(status!=null){
            wrapper.eq("status",status);
        }
        wrapper.orderByAsc("sort");
        carouselIPage=page(carouselPage,wrapper);
        return carouselIPage;
    }

    @Override
    public int addCarousel(CarouselVO carouselVO) {
        Carousel carousel = new Carousel();
        BeanUtils.copyProperties(carouselVO,carousel);
        carousel.setStatus(1);
        carousel.setCarouselLocation(1);
        carousel.setPlatShow(0);
        carousel.setCreateUser(1l);
        carousel.setUpdateUser(1l);
        return this.baseMapper.insert(carousel);
    }

    @Override
    public Carousel editCarousel(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int updateCarousel(CarouselVO carouselVO) {
        Carousel carousel = new Carousel();
        BeanUtils.copyProperties(carouselVO,carousel);
        return this.baseMapper.updateById(carousel);
    }

    @Override
    public int updateStatus(Integer id, Integer status) {
        UpdateWrapper<Carousel> iconUpdateWrapper = new UpdateWrapper<>();
        iconUpdateWrapper.eq("id",id);
        Integer newStatusId = (status==1?0:1);
        iconUpdateWrapper.set("status",newStatusId);
        int update = this.baseMapper.update(null, iconUpdateWrapper);
        return update;
    }

    @Resource
    private AliOssService aliOssService;

    public String upload(MultipartFile file, String pathName) {
        return this.aliOssService.upload(file,pathName);
    }

}
